﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;


namespace NNLInterface
{
    public partial class frmCapabilityBas : NNLInterface.frmBaseBasicEntry
    {
        public frmCapabilityBas()
        {
            InitializeComponent();
            this.layOUT = this.layOut;
            this.barInformation.Visible = false;
            this.ImportButtonVisibility = true;

            //2013-02-28如果设为只读，则只需要以下语句，因某些用户只需要查看
            //this.CmdImport.Visibility = DevExpress.XtraBars.BarItemVisibility.Never;
            //this.EditButtonVisibility = false;
            //this.EditButtonEnabled = false;
            //this.NewButtonVisibility = false;
            //this.NewButtonEnabled = false;
            //this.DeleteButtonVisibility = false;
            //this.DeleteButtonEnabled = false;
            //this.CancelButtonVisibility = false;
            //this.SaveButtonVisibility = false;

           
        }




        private void frmCapabilityBas_Load(object sender, EventArgs e)
        {
            this.iniFormData("vie_CapabilityBas", "itemid");
        }



        #region 重写基窗体方法

        //当listgrid行移动时，将表格中的数据设定到显示控件中
        protected override void subSetData(DataRow dr)
        {

            common.SetGridData(dr, layOut);
        }

        //设定验证条件
        protected override void subSetValidCondition()
        {
            base.subSetValidCondition();
            //设定部门编号不能为空
            DataValid.SetControlValidBlank(this.validData, this.lokItemCode, "料号不能为空！");
            //DataValid.SetControlValidBlank(this.validData, this.txtDeptNo, "最终部门不能为空！");
           
        }

        //设定只读
        protected override void subSetContolRead()
        {
            base.subSetContolRead();
            this.lokItemCode.Properties.ReadOnly = true;
            this.txtDeptNo.Properties.ReadOnly = true;
            //this.txtApsItemNo.Properties.ReadOnly = true;
            //this.txtErpItemNo.Properties.ReadOnly = true;
        }


        //格式化表格各项目格式
        protected override void subSetGridFormat()
        {
            //common.SetGridColumnPercentFormat(this.ListView, new int[] { 3, 4, 5 });
        }

        //更新数据源并设定焦点
        protected override void subSetDataSource()
        {
            common.SetDataSource(this.Name, this.layOut);
            this.lokItemCode.Focus();
        }

        protected override bool CheckSaveData()
        {
            //if(this.numZ1Capability.Value+this.numZ2Capability.Value
            //    + this.numZ3Capability.Value+this.numZ5Capability.Value
            //    +this.numZ7Capability.Value==0)
            //{
            //    DevExpress.XtraEditors.XtraMessageBox.Show("工时不能全部为空,请检查!", common.sysParameter.MessageInf, MessageBoxButtons.OK, MessageBoxIcon.Information);
            //    return false;

            //}

            //如果Z7不为空，则其他的课不能为空
            if(numZ7Capability.Value!=0 && (numZ1Capability.Value!=0 || numZ2Capability.Value!=0 || numZ3Capability.Value!=0 || numZ5Capability.Value!=0 || numZ6Capability.Value!=0))
            {
                DevExpress.XtraEditors.XtraMessageBox.Show("当Z7工时不为0时，其他课别的工时都必须为0!", common.sysParameter.MessageInf, MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;

            }
            return true;
        }

        //导入CSV文件
        protected override void subImportData()
        {

            frmImportCapability frmimport = new frmImportCapability(this);
            frmimport.ShowDialog();
            frmimport = null;
            string strSql = "Select   * From " + this.v_Open_View + "  Order by " + this.v_Field_Key_Name;
            this.SetToolVisible(common.ToolVisible.BssicEntryImport);
            common.subIniGrid(this.ListView, common.enumGridStyle.ViewStyle);
            common.SubShowList(strSql, ListGrid, ListView, false);
            common.GetResourceString(this, this.Name);
            common.subSetCmdControl(common.DataModifyMode.view, (frmBaseToolXC)this, this.ListView.RowCount);
            common.ClsFormControl(this);
            common.SetFormReadOnly(this, true);
            subSetDataSource();
            ListGrid.Focus();
            if (ListView.RowCount >= 1)
            {
                this.ListView.MoveLast();
                //DataRow dt = ((DataView)ListView.DataSource).ToTable().Rows[0];
                DataRow dt = ListView.GetFocusedDataRow();
                subSetData(dt);
            }
        }

        #endregion

        #region 数据更新

        private void  frmCapabilityBas_EditButtononClick(common.DataModifyMode dataMode)
        {
            switch (dataMode)
            {
                case common.DataModifyMode.add:
                    this.strInsertSql = "INSERT INTO CapabilityBas \n"
                                       + "( \n"
                                       + "	ItemCode, \n"
                                       + "	DeptNo, \n"
                                       + "	Z1Capability, \n"
                                       + "	Z2Capability, \n"
                                       + "	Z3Capability, \n"
                                       + "	Z5Capability, \n"
                                       + "	Z6Capability, \n"
                                       + "	Z7Capability, \n"
                                       + "	Remark, \n"
                                       + "	CustNo, \n"
                                       + "	CustItemCode, \n"
                                       + "	UptDate \n"
                                       + ")"
                                       + "values ('"
                                       + this.lokItemCode.Text + "','"
                                       + this.txtDeptNo.Text + "',"
                                       + this.numZ1Capability.Value.ToString() + ","
                                       + this.numZ2Capability.Value.ToString() + ","
                                       + this.numZ3Capability.Value.ToString() + ","
                                       + this.numZ5Capability.Value.ToString() + ","
                                       + this.numZ6Capability.Value.ToString() + ","
                                       + this.numZ7Capability.Value.ToString() + ",'"
                                       + this.txtRemark.Text + "','"
                                       + this.txtCustNo.Text + "','"
                                       + this.txtCustItemCode.Text + "',"
                                       + "getdate()) \n"

                                       //更新总制造工时
                                       + "UPDATE CapabilityBas "
                                       + "  SET ZGS=Z1Capability+Z2Capability+Z3Capability "
                                       + "        +Z5Capability+Z6Capability+ Z7Capability "
                                       + "  WHERE itemcode='" + this.lokItemCode.Text + "'";
                    break;
                case common.DataModifyMode.edit:
                    this.strUpdateSql = " UPDATE CapabilityBas  \n"
                                   + "   SET   "
                                   + "   Z1Capability=" + this.numZ1Capability.Value.ToString() + ","
                                   + "   Z2Capability=" + this.numZ2Capability.Value.ToString() + ","
                                   + "   Z3Capability=" + this.numZ3Capability.Value.ToString() + ","
                                   + "   Z5Capability=" + this.numZ5Capability.Value.ToString() + ","
                                   + "   Z6Capability=" + this.numZ6Capability.Value.ToString() + ","
                                   + "   Z7Capability=" + this.numZ7Capability.Value.ToString() + ","
                                   + "   remark ='" + this.txtRemark.Text + "',"
                                   + "   custNo ='" + this.txtCustNo.Text + "',"
                                   + "   custItemCode ='" + this.txtCustItemCode.Text + "',"
                                   + "   UptDate =getdate() "
                                   + "   WHERE ITEMID=" + txtItemID.Text + "\n"
                        //更新总制造工时
                                   + "UPDATE CapabilityBas "
                                   + "  SET ZGS=Z1Capability+Z2Capability+Z3Capability "
                                  + "  +Z5Capability+Z6Capability+ Z7Capability"
                                   + "  WHERE  ITEMID=" + txtItemID.Text;
                    break;
                default:
                    this.strDeleteSql = string.Format("delete from CapabilityBas  WHERE itemid='{0}'", this.txtItemID.Text);
                                     
                    break;
            }
        }
        #endregion


        /// <summary>
        /// 计算总工时
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void numZ1Capability_EditValueChanged(object sender, EventArgs e)
        {
            DevExpress.XtraEditors.CalcEdit numQty = sender as DevExpress.XtraEditors.CalcEdit;
            this.numZGS.Value = this.numZ1Capability.Value 
                + this.numZ2Capability.Value 
                + this.numZ3Capability.Value
                + this.numZ5Capability.Value
                + this.numZ6Capability.Value
                + this.numZ7Capability.Value;
        }

        private void lokItemCode_EditValueChanged(object sender, EventArgs e)
        {
            if(lokItemCode.Text!="")
            {
                this.txtDeptNo.Text = this.lokItemCode.GetColumnValue("deptNo").ToString();
            }
            else
            {
                this.txtDeptNo.Text = "";
            }
        }






    }
}
